<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/4/9 14:40
 * @version
 */
namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\EverydayReport;
use App\Models\PushMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ReportController extends Controller
{
    public function everyday(Request $request)
    {
        return view('main::report_everyday', \Request::all());
    }

    public function doEveryday(Request $request)
    {
        $data = [];
        $limit  = $request->input('limit', 10);
        $offset = $request->input('offset', 0);
        $date = $request->input('datetime');
        $platform = $request->input('platform');
        $channel  = $request->input('channel');
        $data['total'] = $this->buildQuery($platform,$channel,$date)->count();
        $data['items'] = $this->buildQuery($platform,$channel,$date)->take($limit)->skip($offset)->orderBy('today', 'desc')->get();
        $data['total_recharge_amount'] = (int) $this->buildQuery($platform,$channel,$date)->sum(DB::raw('recharge_amount + mobile_recharge_amount'));
        $data['total_pay_amount'] = $this->buildQuery($platform,$channel,$date)->sum('pay_amount');
        return success($data);
    }

    protected function buildQuery($platform,$channel,$date)
    {
        $query = EverydayReport::query();

        if($platform){
            $query = $query->where('app_platform',$platform);
        }

        if($channel){
            $query = $query->where('app_channel',$channel);
        }

        if($date){
            $query = $query->where('today',$date);
        }

        return $query;
    }
}